package NWish.properties.object3ds
{
	import NWish.Away3d;
	import NWish.Logger;
	import NWish.Misc;
	import NWish.NWEvent;
	import NWish.json.OrderDict;
	
	import away3d.cameras.Camera3D;
	import away3d.containers.ObjectContainer3D;
	import away3d.containers.Scene3D;
	import away3d.primitives.WireframeCube;
	
	import NWish.Interface.ILogger;
	
	
	public class ObjectContainer3DProperties extends Object3DProperties
	{
		public function ObjectContainer3DProperties(parent)
		{
			super(parent);
			
			var keys
			= 
				['visible'
				,'mouseChildren'
				];
			addPropertieKeys('ObjectContainer3D',keys);
			
		}
		
		
		static private var _keysRead
		= 
			[
				//'minX'
				//,'minY'
				//,'minZ'
				//,'maxX'
				//,'maxY'
				//,'maxZ'
				//,
				'scenePosition'
				,'numChildren'
				,'assetType'
			];
		static private var _keysRead1
		= 
			[
				'minX'
				,'minY'
				,'minZ'
				,'maxX'
				,'maxY'
				,'maxZ'
			];
		override public function propertiesToDict(obj,d:OrderDict,read_only = false,saving = false)
		{
			super.propertiesToDict(obj,d,read_only,saving);
			
			if(read_only == false)
			{
				if(saving == false)
					_title('Container',d);
				
				if(obj.parent)
					d.push('parent', obj.parent.name);
				else
					d.push('parent', null);
			}
			else
			{
				_title('Container read only',d);
				
				
				//if(obj.parent)
				//if(obj.geometry.)
				try
				{
					for each(var k in _keysRead1)
					{
						d.push(readMark + k, obj[k]);
					}
				}
				catch(e){}
				
				//
				for each(var k in _keysRead)
				{
					d.push(readMark + k, obj[k]);
				}
				
				var num = obj.numChildren;
				var children:Array = [];
				for(var i = 0; i < num; ++i)
				{
					var  c = obj.getChildAt(i);
					if(c is WireframeCube)
						continue;
					
					children.push(c.name);
				}
				d.push(readMark + 'children', children);
			}
		}
		
		override public function dictToProperties(d,obj,scene:Scene3D,isloading)
		{
			super.dictToProperties(d,obj,scene,isloading);
			
			var parent_name = d['parent'];
			
			if(obj is Camera3D && obj.name == Away3d.SystemCamera)
			{
				if(parent_name && parent_name != '')
					ILogger.log("err : " +  Away3d.SystemCamera + " can not change parent now!");
				
				return;
			}
			
			
			if(parent_name)
			{
				var parent = Misc.getChildByNameInScene(parent_name,scene);
				changeParent(obj,parent);
				return;
			}
			
			
			/*if(obj.parent == null && parent_name == null)
			{
				//obj.parent.removeChild(obj);
				scene.addChild(obj);
				
				ILogger.log("change parent success " + obj.name + "'s parent is Scene" );
				_app.dispatchEvent(new NWEvent(NWEvent.CHANGE_PARENT,obj))
				return;
			}*/
			
			//普通的属性修改 但是并没有设置新的parent,这时parent = ''
			if(obj.parent && parent_name == '')
				return;

			if(obj.parent && parent_name == null)
				return;
			
			if(obj.parent == null)
			{
				scene.addChild(obj);
				_app.dispatchEvent(new NWEvent(NWEvent.CHANGE_PARENT,obj,scene))
				return;
			}
			
			throw new Error();
		}
		
		private function changeParent(obj,parent)
		{
			if(parent == null)
			{
				ILogger.log("err : can not find parent object");
				return;
			}
			if(obj == parent)
			{
				ILogger.log("err : object.parent == object");
				return;
			}
			
			if(parent)
			{
				if(obj.parent != parent)
				{
					if(obj.parent)
						obj.parent.removeChild(obj);
					
					parent.addChild(obj);
					
					ILogger.log("change parent success " + obj.name + "'s parent is " + parent.name);
					_app.dispatchEvent(new NWEvent(NWEvent.CHANGE_PARENT,obj))
					return;
				}
				else
				{
					ILogger.log("objct's parent already is " + parent.name);
				}
			}
		}
		
		
		override public function toString()
		{
			return "ObjectContainer3D";
		}
		
		override public function get type()
		{
			return ObjectContainer3D;
		}
		
		override public function get createEnable()
		{
			return true;
		}
		
		override public function get multiCreateEnable()
		{
			return true;
		}
		
		override public function newType(scene,dict)
		{
			var container:ObjectContainer3D = super.newType(scene,dict);
			
			if(scene && container && _app && _app.isEditor)
			{
				var icon3d = new WireframeCube(10, 10, 10);
				icon3d.name = Away3d.SysIcon3d;
				container.addChild(icon3d);
			}
			return container;
		}
	}
	
}







